package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderCartEventDto;
import com.java110.dto.storeOrder.StoreOrderCartSendDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrderWriteoff.StoreOrderWriteoffDto;
import com.java110.intf.cart.*;
import com.java110.mall.cart.storeOrderEngine.StoreOrderShareAcct;
import com.java110.po.storeOrder.StoreOrderCartEventPo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderCartSendPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrderWriteoff.StoreOrderWriteoffPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "storeOrder.confirmWriteOffOrder")
public class ConfirmWriteOffOrderCmd extends Cmd {

    @Autowired
    private IStoreOrderWriteoffV1InnerServiceSMO storeOrderWriteoffV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private StoreOrderShareAcct storeOrderShareAcct;

    @Autowired
    private IStoreOrderCartEventInnerServiceSMO storeOrderCartEventInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartSendInnerServiceSMO storeOrderCartSendInnerServiceSMOImpl;


    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "woNum", "请求报文中未包含woNum");
        Assert.hasKeyAndValue(reqJson, "shopId", "shopId不能为空");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        StoreOrderWriteoffDto storeOrderWriteoffDto = new StoreOrderWriteoffDto();
        storeOrderWriteoffDto.setWoNum(reqJson.getString("woNum"));
        storeOrderWriteoffDto.setState(StoreOrderWriteoffDto.STATE_WAIT);
        List<StoreOrderWriteoffDto> storeOrderWriteoffDtos =
                storeOrderWriteoffV1InnerServiceSMOImpl.queryStoreOrderWriteoffs(storeOrderWriteoffDto);
        if (ListUtil.isNull(storeOrderWriteoffDtos)) {
            throw new CmdException("订单不存在");
        }
        StoreOrderWriteoffPo storeOrderWriteoffPo = new StoreOrderWriteoffPo();
        storeOrderWriteoffPo.setOrderId(storeOrderWriteoffDtos.get(0).getOrderId());
        storeOrderWriteoffPo.setState(StoreOrderWriteoffDto.STATE_FINISH);
        storeOrderWriteoffV1InnerServiceSMOImpl.updateStoreOrderWriteoff(storeOrderWriteoffPo);

        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setOrderId(storeOrderWriteoffDtos.get(0).getOrderId());
        storeOrderCartDto.setState(StoreOrderCartDto.STATE_STORE_VERIFICATION);
        List<StoreOrderCartDto> storeOrderCartDtos = storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
        if (ListUtil.isNull(storeOrderCartDtos)) {
            return;
        }
        for (StoreOrderCartDto tStoreOrderCartDto : storeOrderCartDtos) {
            StoreOrderCartPo storeOrderCartPo = new StoreOrderCartPo();
            storeOrderCartPo.setState(StoreOrderCartDto.STATE_WAIT_REPAIR);
            storeOrderCartPo.setCartId(tStoreOrderCartDto.getCartId());
            storeOrderCartInnerServiceSMOImpl.updateStoreOrderCart(storeOrderCartPo);
            StoreOrderCartEventDto storeOrderCartEventDto = new StoreOrderCartEventDto();
            storeOrderCartEventDto.setCartId(tStoreOrderCartDto.getCartId());
            storeOrderCartEventDto.setOrderId(tStoreOrderCartDto.getOrderId());
            storeOrderCartEventDto.setEventType(StoreOrderCartEventDto.EVENT_TYPE_EXPEDITING_SENDING);
            List<StoreOrderCartEventDto> storeOrderCartEventDtos = storeOrderCartEventInnerServiceSMOImpl.queryStoreOrderCartEvents(storeOrderCartEventDto);
            String eventObjId = null;
            if (!ListUtil.isNull(storeOrderCartEventDtos)) {
                eventObjId = storeOrderCartEventDtos.get(0).getEventObjId();
            } else {
                eventObjId = tStoreOrderCartDto.getPersonId();
            }
            StoreOrderCartEventPo storeOrderCartEventPo = new StoreOrderCartEventPo();
            storeOrderCartEventPo.setCartId(tStoreOrderCartDto.getCartId());
            storeOrderCartEventPo.setOrderId(tStoreOrderCartDto.getOrderId());
            storeOrderCartEventPo.setEventId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_eventId));
            storeOrderCartEventPo.setEventObjType("S");
            storeOrderCartEventPo.setEventObjId(eventObjId);
            storeOrderCartEventPo.setEventMsg("到店核销完成");
            storeOrderCartEventPo.setShopId(tStoreOrderCartDto.getShopId());
            storeOrderCartEventPo.setEventType(StoreOrderCartEventDto.EVENT_TYPE_EXPEDITING_SENDING_SUCCESS);
            storeOrderCartEventInnerServiceSMOImpl.saveStoreOrderCartEvent(storeOrderCartEventPo);
        }

        //todo 处理派送数据
        updateSendStateToFinish(storeOrderCartDtos.get(0));

        // 分账处理
        StoreOrderDto storeOrderDto = new StoreOrderDto();
        storeOrderDto.setOrderId(storeOrderWriteoffDtos.get(0).getOrderId());
        List<StoreOrderDto> storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);
        Assert.listOnlyOne(storeOrderDtos, "订单不存在");
        storeOrderShareAcct.shareAcct(storeOrderDtos.get(0));

        //修改为分账完成
        StoreOrderPo storeOrderPo = new StoreOrderPo();
        storeOrderPo.setOrderId(storeOrderDto.getOrderId());
        storeOrderPo.setProfitSharing(StoreOrderDto.PROFIT_SHARING_FINISH);
        storeOrderInnerServiceSMOImpl.updateStoreOrder(storeOrderPo);
    }

    /**
     * 派送数据状态修改为完成
     *
     * @param storeOrderCartDto
     */
    private void updateSendStateToFinish(StoreOrderCartDto storeOrderCartDto) {
        int flag;
        StoreOrderCartSendDto storeOrderCartSendDto = new StoreOrderCartSendDto();
        storeOrderCartSendDto.setCartId(storeOrderCartDto.getOrderId());
        storeOrderCartSendDto.setShopId(storeOrderCartDto.getShopId());
        List<StoreOrderCartSendDto> storeOrderCartSendDtos = storeOrderCartSendInnerServiceSMOImpl.queryStoreOrderCartSends(storeOrderCartSendDto);
        if (ListUtil.isNull(storeOrderCartSendDtos)) {
            return;
        }
        StoreOrderCartSendPo storeOrderCartSendPo = new StoreOrderCartSendPo();
        storeOrderCartSendPo.setState("1003");
        storeOrderCartSendPo.setFinishTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        storeOrderCartSendPo.setSendId(storeOrderCartSendDtos.get(0).getSendId());
        flag = storeOrderCartSendInnerServiceSMOImpl.updateStoreOrderCartSend(storeOrderCartSendPo);
        if (flag < 1) {
            throw new IllegalArgumentException("修改派送状态失败");
        }
    }
}
